package rs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rs.creator.BannedTracksListCreator;
import rs.creator.BestTags;
import rs.creator.SimilarityAndTagWeightCreator;
import rs.creator.UserListCreator;
import rs.creator.RecommendedTracksListCreator;
import rs.creator.RecommendedTracksVerifier;
import rs.creator.TagsMapCreator;
import rs.creator.TracksMapCreator;
import rs.domain.RecommendedTrack;
import rs.domain.Tag;
import rs.domain.Track;
import rs.util.Requester;
import rs.util.CanonicalString;
import rs.util.TuningParameters;

/* Classe principale che contiene il metodo main */
public class RS {
	
	public static void main(String[] args) {
		try {
			System.out.println("START");
			long start = System.currentTimeMillis();
			
			String user = new CanonicalString("MangakaFan").toString();
			Requester app = new Requester();
			
			System.out.println("\n--- Retrieve friend/neighbours ---\n");			
			List<String> users = new UserListCreator().create(app, user);
			
			System.out.println("\n--- Retrieve tracks ---\n");	
			Map<String, List<Track>> user2tracks = new TracksMapCreator().create(app, users);
			
			System.out.println("\n--- Retrieve tags ---\n");
			Map<Track, List<String>> track2tags = new TagsMapCreator().create(app, user2tracks);

			System.out.println("\n--- Retrieve similarity ---\n");
			SimilarityAndTagWeightCreator stc = new SimilarityAndTagWeightCreator();
			stc.create(user, user2tracks, track2tags);
			Map<String, Double> tags2weights = stc.getTagsMap();
			Map<String, Double> user2similarity = stc.getUsersMap();
			
			System.out.println("\n--- Retrieve best tags ---\n");
			List<Tag> tag_list = new BestTags().getTags(tags2weights);
			
			System.out.println("\n--- Retrieve recommended tracks ---\n");
			List<RecommendedTrack> rec_tracks = new RecommendedTracksListCreator().create(app, tag_list);

			System.out.println("\n--- Retrieve banned tracks ---\n");
			List<Track> ban_tracks = new BannedTracksListCreator().create(app, user);
			
			System.out.println("\n--- Verify tracks ---\n");
			RecommendedTracksVerifier rtv = new RecommendedTracksVerifier();
			rtv.verify(app, rec_tracks, ban_tracks, user2tracks, user2similarity, user);

			System.out.println("\nEND\n");
			long end = System.currentTimeMillis();
			long time = end - start;
			if (time > 60000) {
				long min = time / 60000;
				long sec = (time % 60000) / 1000;
				if (sec < 10) {
					System.out.println("Time : " + min + ".0" + sec + " min");
				}
				else {
					System.out.println("Time : " + min + "." + sec + " min");
				}
			}
			else {
				long sec = time / 1000;
				System.out.println("Time : " + sec + " sec");
			}
			
			System.out.println("\n\n--- Info ---\n\n");
			
			System.out.println("Recommended Tracks Are:");
			for (RecommendedTrack t : rec_tracks) {
				System.out.println(t.getTrack().getName() + " / " + t.getTrack().getArtist() + " / " + t.getRanking());
			}
			
			System.out.print("\nBest tags are:");
			for (int i = 0; i < TuningParameters.BEST_TAGS_LIMIT && i < tag_list.size(); i++) {
				System.out.print(" [" + tag_list.get(i).getName() + "," + tag_list.get(i).getWeight() + "]");
			}
			System.out.println();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
